﻿<cfscript>
/** 
* @hint "acadmicBase - School"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.senate.schoolListener" 
	output=false 
	{
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 
	
	
	/**
	* @hint "更新学校信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateSchool( required MachII.framework.Event event ) output=false {
		
		var school = "";
		
		var schoolArray = [];
		var hoster = "";
		var typer = "";
		var proper = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* delete all record */
			var sql = "SELECT a.sch_id
					FROM t_school a ";
			var queryObj = new Query( datasource=application.dnsMaster) ;
			var rs_school = queryObj.execute( sql=sql ).getResult();	
			
			for (var a=1; a LTE rs_school.recordCount; a++ ) {
				sql = "DELETE FROM t_school 
							WHERE sch_id = :schoolId ";
				queryObj = new Query( datasource=application.dnsMaster) ;
				queryObj.addParam( name="schoolId", value=rs_school.sch_id, cfsqltype="cf_sql_varchar" );
				queryObj.execute( sql=sql );	
			}
		
		}
		
		transaction {
			
			/* create new entity*/
			sql = "INSERT INTO t_school (
					   sch_id,
					   sch_name,
					   sch_name_eng,
					   sch_address,
					   sch_zipcode,
					   sch_citycode,
					   sch_establish,
					   sch_host,
					   sch_type,
					   sch_prop 
					)VALUES(
						:SchoolID ,
						:SchoolName ,
						:SchoolEnglishName ,
						:SchoolAddress ,
						:SchoolZipCode ,
						:SchoolCityCode ,
						:SchoolEstablish ,
						:HostCode ,
						:TypeCode ,
						:PropCode 
					)";
			queryObj = new Query( datasource=application.dnsMaster) ;
			queryObj.addParam( name="SchoolID", value=event.getArg("SchoolID"), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="SchoolName", value=event.getArg("SchoolName"), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="SchoolEnglishName", value=event.getArg("SchoolEnglishName"), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="SchoolAddress", value=event.getArg("SchoolAddress"), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="SchoolZipCode", value=event.getArg("SchoolZipCode"), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="SchoolCityCode", value= event.getArg("forCounty"), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="SchoolEstablish", value= event.getArg("forYear") & event.getArg("forMonth") , cfsqltype="cf_sql_char" );
			queryObj.addParam( name="HostCode", value=event.getArg("HostCode"), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="TypeCode", value=event.getArg("TypeCode"), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="PropCode", value=event.getArg("PropCode"), cfsqltype="cf_sql_char" );
			queryObj.execute( sql=sql );	
		}
		
	}
	
	/**
	* @hint "初始化学校信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function createSchool( required MachII.framework.Event event ) output=false {
		
		var school = "";
		
		var hoster = "";
		var typer = "";
		var proper = "";
		
		var sql = ""; 
		var queryObj = ""; 
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "INSERT INTO t_school(
						sch_id, sch_name, sch_name_eng,
						sch_address, sch_zipcode, sch_citycode,
						sch_establish, sch_host, sch_type, sch_prop 
					)VALUES(
						:schoolID, :schoolName, :schoolEnglishName, 
						:schoolAddress, :schoolZipCode, :schoolCityCode, 
						:schoolEstablish, :hostCode, :typeCode, :propCode
					)";
					
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="schoolID", value=event.getArg("SchoolID"), cfsqltype="cf_sql_char" ); 
			queryObj.addParam( name="schoolName", value=event.getArg("SchoolName"), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="schoolEnglishName", value=event.getArg("SchoolEnglishName"), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="schoolAddress", value=event.getArg("SchoolAddress"), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="schoolZipCode", value=event.getArg("SchoolZipCode"), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="schoolCityCode", value=event.getArg("forCounty"), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="schoolEstablish", value=event.getArg("forYear") & event.getArg("forMonth"), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="hostCode", value=event.getArg("HostCode"), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="typeCode", value=event.getArg("TypeCode"), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="propCode", value=event.getArg("PropCode"), cfsqltype="cf_sql_char" );
			
			queryObj.execute( sql=sql );
				
		}
	}

	/**
	* @hint "读取学校主办者字典"
	*/
	public function getSchoolHoster() output=false {
		
		var sql = "SELECT sch_host, sch_host_name from t_school_hoster order by sch_host ASC";
		
		var queryObj = new Query( datasource=application.dnsSlave );
		var rs_hoster = queryObj.execute( sql=sql ).getResult(); 
		
		hosterArray = arrayNew(1);
		
		for( var i=1; i LTE rs_hoster.recordCount; i++){
			arrayAppend(hosterArray,[rs_hoster["sch_host"][i],rs_hoster["sch_host_name"][i]]);
		}
		
		return hosterArray;	
	}
	
	/**
	* @hint "读取学校类别字典"
	*/
	public function getSchoolType() output=false {
		
		var sql = "SELECT sch_type, sch_type_name FROM t_school_type ORDER BY sch_type ASC ";
		
		var queryObj = new Query( datasource=application.dnsSlave );
		var rs_type = queryObj.execute( sql=sql ).getResult(); 
		
		typeArray = arrayNew(1);
		
		for( var i=1; i LTE rs_type.recordCount; i++){
			arrayAppend(typeArray,[rs_type["sch_type"][i],rs_type["sch_type_name"][i]]);
		}
		
		return typeArray;	

	}
	
	/**
	* @hint "读取办学性质字典"
	*/
	public function getSchoolProperty() output=false {
		
		var sql = "SELECT sch_prop, sch_prop_name FROM t_school_prop ORDER BY sch_prop ASC ";
		
		var queryObj = new Query( datasource=application.dnsSlave );
		var rs_prop = queryObj.execute( sql=sql ).getResult(); 
		
		propArray = arrayNew(1);
		
		for( var i=1; i LTE rs_prop.recordCount; i++){
			arrayAppend(propArray,[rs_prop["sch_prop"][i],rs_prop["sch_prop_name"][i]]);
		}
		
		return propArray;
		
	}
	
}

</cfscript>